package com.example.wordlistdemo.util;

import android.content.Context;
import android.os.Environment;

import com.example.wordlistdemo.WordEntity;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ExcelHelper {

    private static final String TAG = ExcelHelper.class.getSimpleName();

    public static List<WordEntity> readExcel(Context context, InputStream inputStream) {
        List<WordEntity> resultList = new ArrayList<>();
        try {
//            File file = new File(context.getExternalFilesDir(null), fileName);
//            FileInputStream inputStream = new FileInputStream(file);
           // InputStream inputStream = context.getAssets().open(fileName);
            Workbook workbook = new HSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> iterator = sheet.iterator();
            iterator.next();
            while (iterator.hasNext()) {
                Row currentRow = iterator.next();
                Cell englishCell = currentRow.getCell(0);
                Cell chineseCell = currentRow.getCell(1);
                Cell phoneticCell = currentRow.getCell(2);
                Cell familiarCell = currentRow.getCell(3);
                Cell isHideCell = currentRow.getCell(4);

                String english = convertToString(englishCell);
                String chinese =  chineseCell.getStringCellValue();
                String phonetic = phoneticCell.getStringCellValue();
                int familiar = Integer.parseInt(convertToString(familiarCell));
                int isHide = Integer.parseInt(convertToString(isHideCell));
                WordEntity entity = new WordEntity(english,chinese,phonetic,familiar,isHide);
                resultList.add(entity);
            }
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultList;
    }

    private static  String convertToString(Cell cell) {
        if (cell == null) {
            return "0";
        }

        CellType cellType = cell.getCellType();
        switch (cellType) {
            case STRING:
                return cell.getStringCellValue();
            case BOOLEAN:
                return Boolean.toString(cell.getBooleanCellValue());
            case NUMERIC:
                int cellV = (int)cell.getNumericCellValue();
                return String.valueOf(cellV);
            default:
                return "0";
        }
    }
    public static void exportToExcel( List<WordEntity> wordList) {
        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("WordList");

        // 添加表头
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("English");
        headerRow.createCell(1).setCellValue("Chinese");
        headerRow.createCell(2).setCellValue("Pronunciation");
        headerRow.createCell(3).setCellValue("Familiar");
        headerRow.createCell(4).setCellValue("isHide");

        // 添加数据
        for (int i = 0; i < wordList.size(); i++) {
            Row row = sheet.createRow(i + 1);
            WordEntity word = wordList.get(i);
            row.createCell(0).setCellValue(word.getEnglish());
            row.createCell(1).setCellValue(word.getChinese());
            row.createCell(2).setCellValue(word.getPhonetic());
            row.createCell(3).setCellValue(word.isFamiliar() ? "1" : "0");
            row.createCell(4).setCellValue(word.isHide() ? "1" : "0");
        }

        // 保存Excel文件到Download/aa目录
        try (FileOutputStream fileOut = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/aaa/"+DateUtils.getCurrentTime()+"_word.xls")) {
            workbook.write(fileOut);
            fileOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

